package com.mhc.haval.controller;

import com.mhc.haval.bo.AuditQuotaBO;
import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.dal.model.AuditQuotaDO;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.form.AuditQuotaForm;
import com.mhc.haval.form.PageForm;
import com.mhc.haval.security.model.User;
import com.mhc.haval.service.AuditQuotaService;
import com.mhc.haval.vo.AuditQuotaVO;
import com.subaru.common.entity.BizResult;
import com.subaru.common.query.PageResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author Churry
 * @create 2017-10-11 11:16
 **/
@Slf4j
@RestController
@RequestMapping("/quota")
public class AuditQuotaController {

    @Autowired
    AuditQuotaService auditQuotaService;

    /**
     * 获取审核额度列表
     *
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "获取审核额度列表", notes = "获取审核额度列表")
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public Object getAuditQuotaList(@AuthenticationPrincipal User logedUser,AuditQuotaForm auditQuotaForm) {
        BizResult result = new BizResult();
        Optional<User> userOptional = Optional.ofNullable(logedUser);
        if (userOptional.isPresent()) {
            AuditQuotaBO auditQuotaBO = new AuditQuotaBO();
            BeanUtils.copyProperties(auditQuotaForm, auditQuotaBO);
            PageResult<AuditQuotaDO> auditQuotaDOList = auditQuotaService.getAuditQuotaList(logedUser.getCompanyNo(),auditQuotaBO);
            result.setData(auditQuotaDOList);
            result.setPageSize(auditQuotaBO.getPageSize());
            result.setPageNo(auditQuotaBO.getPageNo());
            result.setTotalCount(auditQuotaDOList.getTotalCount());
            result.setCode(MsgCodeEnum.SUCCESS.getErrCode());
            result.setMessage(MsgCodeEnum.SUCCESS.getErrMsg());
            result.setSuccess(true);
            log.info("获取额度列表成功...");
            return result;
        } else {
            return BizResult.create(MsgCodeEnum.EMPTY_USER.getErrCode(), MsgCodeEnum.EMPTY_USER.getErrMsg());
        }
    }

    /**
     * 获取审批额度详情
     *
     * @param quotaId
     * @return
     */
    @ApiOperation(value = "获取审批额度详情", notes = "获取审批额度详情")
    @RequestMapping(value = {"/detail"}, method = RequestMethod.GET)
    public Object getAuditQuotaDetail(Long quotaId) {
        Optional<Long> quotaIdOptional = Optional.ofNullable(quotaId);
        if (quotaIdOptional.isPresent()) {
            return BizResult.create(BeanCopierUtil.convert(auditQuotaService.getAuditQuotaDetail(quotaId), AuditQuotaVO.class), true, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } else {
            return BizResult.create(MsgCodeEnum.QUERY_FAIL.getErrCode(), MsgCodeEnum.QUERY_FAIL.getErrMsg());
        }
    }

    /**
     * 增加或修改审批额度
     *
     * @param auditQuotaForm
     * @return
     */
    @ApiOperation(value = "增加或修改审批额度", notes = "增加或修改审批额度")
    @ApiImplicitParam(name = "auditQuotaForm", value = "审批额度信息", required = true, paramType = "body", dataType = "AuditQuotaForm")
    @RequestMapping(value = {"/addOrModifyAuditQuota"}, method = RequestMethod.POST)
    public Object addOrModifyAuditQuota(@RequestBody AuditQuotaForm auditQuotaForm,@AuthenticationPrincipal User logedUser) {
        try {
            AuditQuotaBO auditQuotaBO = new  AuditQuotaBO();
            BeanUtils.copyProperties(auditQuotaForm, auditQuotaBO);
            auditQuotaBO.setCompanyNo(logedUser.getCompanyNo());
            return BizResult.create(auditQuotaService.addOrModifyAuditQuota(auditQuotaBO), MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        }catch (HavalCommonException e) {
            log.error("addOrModifyAuditQuota HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }

    }

    /**
     * 删除审批额度
     * @param quotaId
     * @return
     */
    @ApiOperation(value = "删除审批额度", notes = "根据id来审批额度")
    @ApiImplicitParam(name = "quotaId", value = "额ID", required = true, paramType = "path", dataType = "Long")
    @RequestMapping(value = "/delete/{quotaId}", method = RequestMethod.GET)
    public BizResult deleteAuditQuota(@PathVariable Long quotaId) {
        try {
            Boolean deletedFlag = auditQuotaService.deleteAuditQuota(quotaId);
            return BizResult.create(deletedFlag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (Exception e) {
            log.error("删除部门异常: " + e.getMessage(), e);
            return BizResult.create(MsgCodeEnum.EXCEPTION.getErrCode(), MsgCodeEnum.EXCEPTION.getErrMsg());
        }
    }

}
